#include <QtGui/QApplication>
#include <QDBusConnection>
#include <QDebug>
#include <QObject>
#include "qmlwindow.h"
#include <QProcess>
#include "dbbusqmlapp.h"
#include "../../DbusInterfaces/genericappadaptor.h"

int main(int argc, char *argv[])
{
    QApplication::setGraphicsSystem("raster");
    QApplication a(argc, argv);

    QString home = "/socMDC/KdeadMouse";
    QString appName = "testapp";
    QString qmlfile = "main.qml";

    if(argc>1){
        appName = argv[1];
    }

    if(argc>=3){
        qmlfile = argv[2];
    }

    if(argc>=4){
        home = argv[3];
    }

    qmlfile = home + "/apps/" + appName +"/qml/" +qmlfile;



    QDBusConnection connection = QDBusConnection::sessionBus();

    QString pwd = QApplication::applicationDirPath();
    qDebug()<< "dirpath:"<<pwd;
    qDebug()<< "home:"<<home;
    qDebug()<< "appName:"<<appName;
    qDebug()<< "qmlfile:"<<qmlfile;

    DbbusQmlApp qmlApp(qmlfile,appName);

    QObject::connect(&a,SIGNAL(focusChanged(QWidget * , QWidget *)),&qmlApp,SLOT(focusAppChanged(QWidget * , QWidget *)));

    new GenericAppAdaptor(&qmlApp);

    QString serviceName = "org.kdeadmouse.apps."+ appName;
    bool rel = connection.registerService(serviceName);
    qDebug()<<"register"<<serviceName<<"==>"<< rel;

    rel = connection.registerObject("/generic/"+appName,&qmlApp);
    qDebug()<<"register /generic/"+ appName +" object"<< rel;

    qmlApp.show();

    return a.exec();
}
